/**
 * 
 */
package org.swing.utility.system.license;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.swing.utility.system.stream.StreamHelper;

/**
 * @author lqnhu
 *
 */
public class LicenseChanger {

	private static final String LICENSE_FILE_PATH = "D:\\kamlesh\\sahi\\trunk\\config\\license.txt";
	private static final String LICENSE_FILE_CHANGE = "D:\\kamlesh\\sahi\\trunk\\htdocs";

	public static void changeLicense(final File f) throws Exception {
		if (f.isDirectory()) {
			File[] files = f.listFiles();
			int len = files.length;
			for (int i = 0; i < len; i++) {
				File file = files[i];
				changeLicense(file);
			}
		} else if (f.getName().endsWith(".java") || f.getName().endsWith(".js")) {
			String s = new String(StreamHelper.readFileAsString(f));
			if (s.indexOf("Copyright  2006  V Narayan Raman") != -1
					&& s.indexOf("LicenseChanger") == -1) {
				System.out.println(f.getName());
				int startIx = s.indexOf("/**");
				if (startIx == -1) {
					return;
				}
				int endIx = s.indexOf("*/");
				StringBuilder sb = new StringBuilder();
				sb.append(s.substring(0, startIx));
				sb.append(new String(StreamHelper.readFileAsString(LICENSE_FILE_PATH)));
				sb.append(s.substring(endIx + 2));
				// String newS = s.replaceAll("[$]GPL_License[$]", new
				// String(Utils.readFile("C:\\my\\sahi\\config\\gpl_inc.txt")));
				FileOutputStream fos = new FileOutputStream(f);
				fos.write(sb.toString().getBytes());
				fos.close();
			}
		}
	}

	public static void main(String[] args) throws IOException {
		try {
			LicenseChanger.changeLicense(new File(LICENSE_FILE_CHANGE));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
